package com.ilxqx.generator.core.mysql;

import com.google.common.collect.Lists;
import com.ilxqx.generator.config.ConfigProperties;
import com.ilxqx.generator.core.AbstractRawTableInfoParser;
import com.ilxqx.generator.po.RawTable;
import com.ilxqx.generator.po.RawTableField;
import com.ilxqx.generator.po.RawTableKey;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * mysql解析表原始数据实现类
 * @author venus
 */
public class MysqlRawTableInfoParser extends AbstractRawTableInfoParser {

    /**
     * 非负标识
     */
    private static final String UNSIGNED_FLAG = "unsigned";

    /**
     * 查询表原始信息
     *
     * @param tableName 表名
     * @return 原始信息
     */
    @Override
    protected String getTableInfoQuerySql(String tableName) {
        return String.format("show table status from `%s` like '%s'", ConfigProperties.INSTANCE.getDatabase(), tableName);
    }

    /**
     * 获取表字段信息的查询SQL
     *
     * @param tableName 表名
     * @return 查询SQL
     */
    @Override
    protected String getTableFieldsInfoQuerySql(String tableName) {
        return String.format("show full columns from `%s`", tableName);
    }

    /**
     * 获取表键信息的查询SQL
     *
     * @param tableName 表名
     * @return 查询SQL
     */
    @Override
    protected String getTableKeysInfoQuerySql(String tableName) {
        return String.format("show keys from `%s`", tableName);
    }

    /**
     * 解析原始表信息
     *
     * @param results SQL查询结果
     * @return 原始表信息对象
     */
    @Override
    protected RawTable parseTableInfo(List<Map<String, Object>> results) {
        Assert.isTrue(results.size() == 1, "查询的表数据不正确");
        Map<String, Object> info = results.get(0);
        RawTable rawTable = new RawTable();
        rawTable.setTableName((String) info.get("Name"));
        rawTable.setTableComment((String) info.get("Comment"));
        return rawTable;
    }

    /**
     * 解析原始表字段
     *
     * @param results SQL查询的结果
     * @return 表字段原始信息
     */
    @Override
    protected List<RawTableField> parseTableFields(List<Map<String, Object>> results) {
        List<RawTableField> rawTableFields = Lists.newArrayListWithCapacity(16);
        for (Map<String, Object> result : results) {
            RawTableField rawTableField = new RawTableField();
            rawTableField.setFieldName((String) result.get("Field"));
            String rawType = (String) result.get("Type");
            rawTableField.setFieldType(StringUtils.substringBefore(rawType, "("));
            String size = StringUtils.substringBetween(rawType, "(", ")");
            if (size != null) {
                try {
                    rawTableField.setFieldSize(Integer.parseInt(size));
                } catch (NumberFormatException e) {
                    rawTableField.setFieldSize(0);
                }
            }
            rawTableField.setUnsigned(StringUtils.containsIgnoreCase(rawType, UNSIGNED_FLAG));
            rawTableField.setKey((String) result.get("Key"));
            rawTableField.setNullable((String) result.get("Null"));
            rawTableField.setFieldComment((String) result.get("Comment"));
            rawTableFields.add(rawTableField);
        }
        return rawTableFields;
    }

    /**
     * 解析原始表键信息
     *
     * @param results SQL查询的结果
     * @return 表键原始信息
     */
    @Override
    protected List<RawTableKey> parseTableKeys(List<Map<String, Object>> results) {
        List<RawTableKey> rawTableKeys = Lists.newArrayList();
        for (Map<String, Object> result : results) {
            RawTableKey rawTableKey = new RawTableKey();
            rawTableKey.setFieldName((String) result.get("Column_name"));
            rawTableKey.setKeyName((String) result.get("Key_name"));
            rawTableKey.setSeq(Integer.parseInt(result.get("Seq_in_index").toString()));
            rawTableKeys.add(rawTableKey);
        }
        return rawTableKeys;
    }

}
